<template>
  <div class="app-container">
    <div class="content">
      <div class="lgm-search">
        <el-form
          ref="form"
          :model="searchParams"
          label-width="100px"
          class="search-form"
        >
          <el-form-item label="仓库名称">
            <el-input
              v-model="searchParams.name"
              placeholder="请输入仓库名称"
            ></el-input>
          </el-form-item>
          <el-row>
						<el-button @click="
                     page = 1;
                     warehouseAdminFn();
                   " type="success" class="lgm-search-btn" icon="el-icon-search">搜索</el-button>
						<el-button class="lgm-reset-btn" icon="el-icon-refresh" @click="reset">重置</el-button>
					</el-row>
        </el-form>
      </div>

      <div class="table-header-btn">
        <el-button
          type="success"
          plain
          size="small"
          class="lgm-add-btn"
          icon="el-icon-plus"
          @click="addList"
          >新增</el-button
        >
       
      </div>

      <div class="table">
        <el-table :data="dataList" border>
        
          <el-table-column
            prop="code"
            label="仓库编码"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="仓库名称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="address"
            label="仓库位置"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="person_name"
            label="仓库联系人"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="person_phone"
            label="联系电话"
            align="center"
          ></el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="280"
          >
            <template slot-scope="scope">
              <div class="flex">
                <el-button
                  size="mini"
                  type="primary"
                  plain
                  @click="handleEdit(scope.$index, scope.row)"
                >
                  <i class="el-icon-edit"></i> 修改
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  plain
                  @click="handleDelete(scope.$index, scope.row)"
                >
                  <i class="el-icon-delete"></i> 删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="lgm-page-wrap">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :page-sizes="[10, 100, 200, 300, 400]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog title="仓库" :visible.sync="dialogVisible" center width="500px">
      <el-form
        :model="detailForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="detailForm"
      >
        <el-form-item
          label="仓库编码"
          prop="code"
          style="width: 100%"
        >
          <el-input v-model="detailForm.code"></el-input>
        </el-form-item>
        
        <el-form-item
          label="仓库名称"
          prop="name"
          style="width: 100%"
        >
          <el-input v-model="detailForm.name"></el-input>
        </el-form-item>
        <el-form-item
          label="仓库地址"
          prop="address"
          style="width: 100%"
        >
          <el-input v-model="detailForm.address"></el-input>
        </el-form-item>
        <el-form-item
          label="仓库联系人"
          prop="person_name"
          style="width: 100%"
        >
          <el-input v-model="detailForm.person_name"></el-input>
        </el-form-item>
        <el-form-item
          label="仓库联系人电话"
          prop="person_phone"
          style="width: 100%"
        >
          <el-input v-model="detailForm.person_phone"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button class="lgm-reset-btn" @click="dialogVisible = false"
          >取消</el-button
        >
        <el-button type="primary" @click="dialogConfirm" class="lgm-search-btn"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { warehouseAdmin } from "@/api/admin";
export default {
  name: "StyleManage",
  data() {
    return {
      dialogVisible: false,
      searchParams: {
        name: "",
      },
      dataList: [],
      page: 1,
      page_size: 10,
      total: 0,
      detailForm:{
        code:"",
        name:"",
        address:"",
        person_name:"",
        person_phone:"",

      }
    };
  },
  mounted(){
    this.warehouseAdminFn();

  },
  methods: {
    handleSizeChange(value) {
      this.page_size = value;
      this.warehouseAdminFn();
    },
    handleCurrentChange(value) {
      this.page = value;
      this.warehouseAdminFn();
    },
    warehouseAdminFn() {
      let obj = this.searchParams;

      delete obj.time1;
      warehouseAdmin(
        {
          page: this.page,
          page_size: this.page_size,
          ...obj,
        },
        "GET"
      ).then((e) => {
        console.log(e.data.list);
        this.dataList = e.data.list;
        this.total = e.data.total;
      });
    },
    dialogConfirm() {
      console.log(this.detailForm);
      warehouseAdmin(
        this.detailForm,
        this.detailForm.id ? "PATCH" : "POST",
        this.detailForm.id
      ).then((e) => {
        this.dialogVisible = false;
        this.seach();
      });
    },

    seach() {
      this.page = 1;
      this.warehouseAdminFn();
    },
    reset() {
      for (let i in this.searchParams) {
        this.searchParams[i] = null;
      }
      this.seach();
    },

    addList() {
      this.detailForm = {
        code:"",
        name:"",
        address:"",
        person_name:"",
        person_phone:"",
      };
      this.dialogVisible = true;
    },
    handleEdit(idx, item) {
      warehouseAdmin({ id: item.id }, "GET", item.id).then((e) => {
        this.detailForm = item;

        this.dialogVisible = true;
      });
    },
    handleDelete(index, item) {
      this.$confirm("是否确认删除选中的?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "submitDeleteBtn",
        type: "warning",
      }).then(() => {
        warehouseAdmin({}, "DELETE", item.id).then((e) => {
          this.seach();
        });

        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.app-container {
  .content {
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
  }

  .lgm-search {
    ::v-deep input {
      width: 210px;
    }
  }

  .content {
  }
}
</style>
